package com.cw.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cw.data.ConstantEnum;
import com.cw.data.Result;
import com.cw.pojo.TbGoods;
import com.cw.pojo.TbItemCat;
import com.cw.service.GoodService;
import com.cw.service.ItemService;
import com.google.gson.Gson;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("goodStatusController")
public class GoodStatusController {

    @Reference
    private ItemService itemService;

    @Reference
    private GoodService goodService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("toGoods")
    public String toGoods(Model model) {

        List<TbItemCat> itemCats = itemService.getAllItem();

        model.addAttribute("it", itemCats);

        return "admin/goods";
    }

    @RequestMapping("getGoodsInfo")
    @ResponseBody
    public Result getGoodsInfo(String goodsName) {

        try {

            List<TbGoods> goodsList = goodService.getGoodsInfo(goodsName);

            return Result.result(ConstantEnum.SUCCESS).setData(goodsList);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("checkStatu")
    @ResponseBody
    public Result checkStatu(Long[] ids, String status) {

        try {

            goodService.checkStatu(ids, status);

            if (status.equals("1")) {

                rocketMQTemplate.convertAndSend("Goods", new Gson().toJson(ids));
            }

            return Result.result(ConstantEnum.SUCCESS).setData("审核成功");

        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR).setData("审核异常");
        }
    }
}
